package cz.schovjan.proj.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import cz.schovjan.proj.dao.IMemberDao;
import cz.schovjan.proj.entity.MemberEntity;

/**
 * Implementace IMemberDao.
 * @author schovjan
 *
 */
@Repository
public class JpaMemberDao implements IMemberDao {

	private static final String FIND_ALL = "select m from MemberEntity as m";
	
	
	@PersistenceContext
	private EntityManager em;
	
	
	@Override
	public void add(MemberEntity me) {
		em.persist(me);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<MemberEntity> findAll() {
		return em.createQuery(FIND_ALL).getResultList();
	}

	
	@Override
	public MemberEntity find(Integer id) {
		return em.find(MemberEntity.class, id);
	}

	
	@Override
	public void delete(Integer id) {
		MemberEntity me = find(id);
		if (me != null) {
			em.remove(me);
		}
	}
	

	@Override
	public void save(MemberEntity me) {
		em.merge(me);
	}

}
